package com.sl.au.service.impl;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.sl.au.entity.EventType;
import com.sl.au.entity.Part;
import com.sl.au.repository.PartBaseInfoRepository;
import com.sl.au.repository.PartRepository;
import com.sl.au.service.PartService;

@Service("PartService")
@Transactional
public class PartServiceImpl extends BaseServiceImpl implements PartService{
	
	@Autowired
	PartRepository partRepository;
	
	@Autowired
	PartBaseInfoRepository partBaseInfoRepository;
	
	@Resource
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
	
	@Override
	public PartRepository getRepository() {
		return this.partRepository;
	}
	
	@Override
	public List<Map<String, Object>> getNodeMap(List<Part> node) {
		List<Map<String, Object>> nodeMap = new ArrayList<Map<String, Object>>();
		for (Part p : node) {
			if (null != p) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("id", p.getId());
				map.put("parentId", p.getPartParent());
				map.put("name", String.valueOf((p.getPartOrder())/2)+p.getPartName());
				if (StringUtils.hasText(p.getPartParent())) {
					map.put("isParent", p.getPartParent());
				}
				map.put("level", p.getPartLevel().getExtended1());
				map.put("partType", p.getPartType().getValue());
				map.put("open", false);
				
				String iconAddress = p.getIconAddress();
				if(iconAddress == null || iconAddress.equals("")) {
					iconAddress = "/wcsi/img/defaultPart.png";
				}else {
					iconAddress = "/wcsi/"+ iconAddress;
				}
				map.put("icon", iconAddress);
				
				nodeMap.add(map);
			}
		}
		return nodeMap;
	}


	@Override
	public List<Part> getAllNodes() {
		List<Part> nodes = new ArrayList<Part>();
		nodes = partRepository.findAllNodes();
		return nodes;
	}
	
	/*********** 获取未绑定节点 *************/
	@Override
	public List<Part> getPartByKey(int start, int length, String key, String orderName, String dir) {
		Page<Part> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		if(key != null && org.springframework.util.StringUtils.hasText(key)) {
			pages = partRepository.getPartByKey(key, pageable);
		}else {
			pages = partRepository.getPart(pageable);
		}
		return pages.getContent();
	}

	@Override
	public long getPartByKeyCount(String key) {
		long count = 0;
		if(key != null && org.springframework.util.StringUtils.hasText(key)) {
			count = partRepository.getPartByKeyCount(key);
		}else {
			count = partRepository.getPartCount();
		}
		return count;
	}
	
	/************ 获取子节点 ************/
	@Override
	public List<Part> getChildrenPartByKey(int start, int length, String id, String key, String orderName, String dir) {
		Page<Part> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		if(StringUtils.hasText(key)) {
			pages = partRepository.findByPartParentAndKey(id, key, pageable);
		}else {
			pages = partRepository.findByPartParent(id,pageable);
		}
		return pages.getContent();
	}

	@Override
	public int getChildrenPartByKeyCount(String id, String key) {
		int count = 0;
		Pageable pageable = new PageRequest(0, Integer.MAX_VALUE);
		if(StringUtils.hasText(key)) {
			count = partRepository.findByPartParentAndKey(id, key, pageable).getContent().size();
		}else {
			count =  partRepository.findByPartParent(id,pageable).getContent().size();
		}
		return count;
	}
	
	/**********************************/
	
	@Override
	public void deleteNodes(String[] ids) {
//		if (ids.length > 0) {
//			for (String id : ids) {
//				if (StringUtils.hasText(id)) {
//					partRepository.setPart(ids);
//					partBaseInfoRepository.removePart(ids);
//				}
//			}
//		}
		partRepository.setPart(ids);
		cleanPartInfo(ids);
	}
	
	/**
	 * 清除部件的部件层级信息
	 * @param id
	 */
	public void cleanPartInfo(String[] ids) {
		partBaseInfoRepository.removePart(ids);
	}

	@Override
	public void setPartOrder(String groupParent) {
		List<Part> parts = partRepository.getPartsByParent(groupParent);
		if(parts != null) {
			Part part = null;
			for(int i=0;i<parts.size();i++) {
				part = parts.get(i);
				part.setPartOrder(i*2);
				partRepository.save(part);
			}
		}
	}

	@Override
	public String getPartIdentify(String id) {
		Part part = partRepository.findOne(id);
		String Identify = part.getPartIdentify();
		String parent = part.getPartParent();
		if(!"0".equals(parent)) {
			Identify = this.getPartIdentify(parent) + Identify;
		}
		return Identify;
	}

	@Override
	public List<Part> findByParentId(String parentId) {
		
		return partRepository.findByParentId(parentId);
	}

	@Override
	public List<Map<String, Object>> getNextNode(String id) {
		//参数设置
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		
		List<Map<String, Object>> partList = new ArrayList<Map<String, Object>>();
		String partSQL = "SELECT p.id , p.partName , (select count(1) from sl_dcms_part pp where pp.partParent = p.id) as isParent " + 
				" FROM" + 
				" sl_dcms_part p " + 
				" WHERE p.partParent = :id "+
				" GROUP BY p.partOrder asc ";
		partList = namedParameterJdbcTemplate.queryForList(partSQL,params);	
		return partList;
	}

	@Override
	public Part findByName(String name) {
	
		return partRepository.findByPartName(name);
	}
	

}
